package org.apache.oozie.tools.workflowgenerator.client.widget.action;

import org.apache.oozie.tools.workflowgenerator.client.OozieWorkflowGenerator;
import org.apache.oozie.tools.workflowgenerator.client.property.action.FSPropertyTable;
import org.apache.oozie.tools.workflowgenerator.client.widget.NodeWidget;

import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;

public class FSActionWidget extends NodeWidget {

    public FSActionWidget(OozieWorkflowGenerator g) {
        super(g);
        this.setStyleName("oozie-FSActionWidget");
        initPropertyTable();
    }

    /**
     * Initialize property table
     */
    protected void initPropertyTable() {

        table = new FSPropertyTable(this);
        this.generator.setPropertyTable(table);

    }

    /**
     * Initialize event handler
     */
    @Override
    protected void initEvent() {

        // Register MouseDown Event to update property table of the selected
        // action
        this.addMouseDownHandler(new MouseDownHandler() {

            @Override
            public void onMouseDown(MouseDownEvent event) {
                FSActionWidget.this.generator.setPropertyTable(FSActionWidget.this.table);
                FSActionWidget.this.updateOnSelection();
            }
        });
    }

    /**
     * Update current lists of created node widgets when clicked in workflow
     * design panel
     */
    public void updateOnSelection() {
        table.updateWidgetList();
        table.updateWidgetDropDown();
        table.updateErrorDropDownList();
    }

    /**
     * Return a name of node widget
     */
    @Override
    public String getName() {
        return table.getName();
    }

    /**
     * Set a name of node widget
     *
     * @param n name
     */
    public void setName(String n) {
        table.setName(n);
    }

    /**
     * Generate xml elements of fs action and attach them to xml doc
     */
    @Override
    public void generateXML(Document doc, Element root, NodeWidget next) {
        table.generateXML(doc, root, next);

    }
}
